#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=50005;

int n,m;
int a[N],d[N];

int find(int x){
    if(x==a[x]) return x;
    int y = find(a[x]);
    d[x] = (d[x] + d[a[x]])%3;
    a[x] = y;
    return y;
}

int ans = 0;
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++){
        a[i] = i;
        d[i] = 0;
    }
    int op,x,y;
    for(int i=1;i<=m;i++){
        scanf("%d%d%d",&op,&x,&y);
        if(x>n||y>n){
            ans++;
            continue;
        }
        int rx = find(x);
        int ry = find(y);
        if(op==1){
            if(rx!=ry){
                a[ry] = rx;
                d[ry] = (d[x]-d[y]+3)%3;
            }
            else if(d[x]!=d[y]){
                ans++;
            }
        }
        else if(op==2){
            if(rx!=ry){
                a[ry] = rx;
                d[ry] = (d[x]-d[y]-1+3)%3;
            }
            else if( (d[x]+2)%3!=d[y] ){
                ans++;
            }
        }
    }
    printf("%d\n",ans);
    return 0;
}